package proiect.fis.UI.tabbedPane;

import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
import java.util.List;
import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;

/**
 *
 * @author Boogie
 * 
 */
public class TabbedPane extends JTabbedPane {

    private List<TabbedPaneListener> listners;
    
    private class TabComponent extends JPanel {

        private JTabbedPane pane;

        public TabComponent(JTabbedPane pane) {
            super(new FlowLayout(FlowLayout.LEFT, 0, 0));
            if (pane == null) {
                throw new NullPointerException("TabbedPane is null");
            }


            this.pane = pane;
            setOpaque(false);

            JLabel label = new JLabel() {

                @Override
                public String getText() {
                    int i = TabComponent.this.pane.indexOfTabComponent(TabComponent.this);
                    if (i != -1) {
                        return TabComponent.this.pane.getTitleAt(i);
                    }
                    return null;
                }
            };

            add(label);

            label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));

            JButton button = new TabButton();
            add(button);

            setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
        }

        private class TabButton extends JButton implements ActionListener {

            public TabButton() {
                int size = 17;
                setPreferredSize(new Dimension(size, size));
                setToolTipText("close this tab");
                setUI(new BasicButtonUI());
                setContentAreaFilled(false);

                setFocusable(false);
                setBorder(BorderFactory.createEtchedBorder());
                setBorderPainted(false);
                addMouseListener(buttonMouseListener);
                setRolloverEnabled(true);
                addActionListener(TabButton.this);
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                int i = pane.indexOfTabComponent(TabComponent.this);
                if (i != -1) {
                    TabbedPane.this.tabClosing(pane.getComponentAt(i),i);
                    pane.remove(i);

                }
            }

            @Override
            public void updateUI() {
            }

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g.create();

                if (getModel().isPressed()) {
                    g2.translate(1, 1);
                }
                g2.setStroke(new BasicStroke(2));
                g2.setColor(Color.DARK_GRAY);
                if (getModel().isRollover()) {
                    g2.setColor(Color.MAGENTA);
                }
                int delta = 6;
                g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
                g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
                g2.dispose();
            }
        }
        private final MouseListener buttonMouseListener = new MouseAdapter() {

            @Override
            public void mouseEntered(MouseEvent e) {
                Component component = e.getComponent();
                if (component instanceof AbstractButton) {
                    AbstractButton button = (AbstractButton) component;
                    button.setBorderPainted(true);
                }
            }

            @Override
            public void mouseExited(MouseEvent e) {
                Component component = e.getComponent();
                if (component instanceof AbstractButton) {
                    AbstractButton button = (AbstractButton) component;
                    button.setBorderPainted(false);
                }
            }
        };
    }
    

    public TabbedPane() {
        listners = new LinkedList<>();
    }


    @Override
    public void addTab(String string, Component cmpnt) {
        super.addTab(string, cmpnt);
        this.setTabComponentAt(this.getTabCount() - 1, new TabComponent(this));
        this.tabOpened(cmpnt,this.getTabCount() - 1);
        
    }
    public void addTabPanel(String name, JPanel tab){
        this.addTab(name,tab);
    }
    public void addTabbedPaneListener(TabbedPaneListener listener){
        listners.add(listener);
    }
    public void removeTabbedPaneListener(TabbedPaneListener listener){
        listners.remove(listener);
    }
    private void tabClosing(Component component,int index) {
        for (TabbedPaneListener tabbedPaneListener : listners) {
            tabbedPaneListener.tabClosed(component,index);
        }
    }

    private void tabOpened(Component component,int index) {
        for (TabbedPaneListener tabbedPaneListener : listners) {
            tabbedPaneListener.tabOpened(component,index);
        }
    }
}
